/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = bytebuff_get_long.c                                */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/13 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    uint64_t l_data = 0;
    int i_len = 8;
    LITTLE_BIG_ENDIAN i_endian = BYTE_LITTLE_ENDIAN;

    i_ret = bytebuff_get_long(p_packet, i_len, &l_data, i_endian);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    return;
}

static void case2(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    uint64_t l_data = 0;
    int i_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_LITTLE_ENDIAN;
    Frlist_node *p_node = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;

    p_packet = bytebuff_creat();
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 20;
    p_packet->ui_remaining = 20;
    p_packet->ui_skip_len = 0;

    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = (char *)frmemdup("\x03\x04\x05\x06\x00\x00\x01\x40\x00\x00",10);
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);

    //添加第2个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = NULL;
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);

    i_ret = bytebuff_get_long(p_packet, i_len, &l_data, i_endian);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);

    bytebuff_free(&p_packet);
    return;
}

static void case3(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    uint64_t l_data = 0;
    int i_len = 8;
    LITTLE_BIG_ENDIAN i_endian = BYTE_LITTLE_ENDIAN;
    Frlist_node *p_node = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;


    p_packet = bytebuff_creat();
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 20;
    p_packet->ui_remaining = 20;
    p_packet->ui_skip_len = 0;

    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = (char *)frmemdup("\x03\x04\x05\x06\x00\x00\x01\x40\x00\x00",10);
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    p_packet->p_node_index = p_node;

    //添加第2个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = NULL;
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);

    i_ret = bytebuff_get_long(p_packet, i_len, &l_data, i_endian);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_EQUAL(l_data,4611967493505090563);
    CU_ASSERT_EQUAL(p_packet->ui_all_len,20);
    CU_ASSERT_EQUAL(p_packet->ui_remaining,12);
    CU_ASSERT_EQUAL(p_packet->pos_index,0);
    CU_ASSERT_EQUAL(p_packet->ui_skip_len,0);
    //确认当前解析节点
    p_node_tmp = p_packet->p_node_index;
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,8);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x03\x04\x05\x06\x00\x00\x01\x40\x00\x00",10) == 0);
    }

    //确认第一个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,8);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x03\x04\x05\x06\x00\x00\x01\x40\x00\x00",10) == 0);
    }
    //确认第二个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 1);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_PTR_NULL(p_bytebuffer_tmp->p_buf);
    }
    
    bytebuff_free(&p_packet);
    return;
}

static void case4(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    uint64_t l_data = 0;
    int i_len = 8;
    LITTLE_BIG_ENDIAN i_endian = BYTE_LITTLE_ENDIAN;
    Frlist_node *p_node = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;

    p_packet = bytebuff_creat();
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 20;
    p_packet->ui_remaining = 17;
    p_packet->ui_skip_len = 0;

    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = (char *)frmemdup("\x00\x00\x00\x03\x04\x05\x06\x00\x00\x01",10);
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 3;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    p_packet->p_node_index = p_node;

    //添加第2个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = (char *)frmemdup("\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00",10);
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);

    i_ret = bytebuff_get_long(p_packet, i_len, &l_data, i_endian);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_EQUAL(l_data,4611967493505090563);
    CU_ASSERT_EQUAL(p_packet->ui_all_len,20);
    CU_ASSERT_EQUAL(p_packet->ui_remaining,9);
    CU_ASSERT_EQUAL(p_packet->pos_index,1);
    CU_ASSERT_EQUAL(p_packet->ui_skip_len,0);
    //确认当前解析节点
    p_node_tmp = p_packet->p_node_index;
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,1);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00",10) == 0);
    }

    //确认第一个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x00\x00\x00\x03\x04\x05\x06\x00\x00\x01",10) == 0);
    }
    //确认第二个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 1);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,1);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00",10) == 0);
    }


    bytebuff_free(&p_packet);
    return;
}

static void case5(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    uint64_t l_data = 0;
    int i_len = 8;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    Frlist_node *p_node = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;


    p_packet = bytebuff_creat();
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 20;
    p_packet->ui_remaining = 17;
    p_packet->ui_skip_len = 0;

    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = (char *)frmemdup("\x00\x00\x00\x40\x01\x00\x00\x06\x05\x04",10);
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 3;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    p_packet->p_node_index = p_node;

    //添加第2个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = (char *)frmemdup("\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00",10);
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);

    i_ret = bytebuff_get_long(p_packet, i_len, &l_data, i_endian);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_EQUAL(l_data,4611967493505090563);
    CU_ASSERT_EQUAL(p_packet->ui_all_len,20);
    CU_ASSERT_EQUAL(p_packet->ui_remaining,9);
    CU_ASSERT_EQUAL(p_packet->pos_index,1);
    CU_ASSERT_EQUAL(p_packet->ui_skip_len,0);
    //确认当前解析节点
    p_node_tmp = p_packet->p_node_index;
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,1);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00",10) == 0);
    }

    //确认第一个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x00\x00\x00\x40\x01\x00\x00\x06\x05\x04",10) == 0);
    }
    //确认第二个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 1);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,1);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00",10) == 0);
    }

    bytebuff_free(&p_packet);
    return;
}

static void case6(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    uint64_t l_data = 0;
    int i_len = 2;
    LITTLE_BIG_ENDIAN i_endian = BYTE_LITTLE_ENDIAN;
    Frlist_node *p_node = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;


    p_packet = bytebuff_creat();
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 20;
    p_packet->ui_remaining = 11;
    p_packet->ui_skip_len = 0;

    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = (char *)frmemdup("\x00\x00\x00\x40\xE2\xC1\xCD\x06\x05\x04",10);
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 9;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    p_packet->p_node_index = p_node;

    //添加第2个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = (char *)frmemdup("\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00",10);
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);

    i_ret = bytebuff_get_long(p_packet, i_len, &l_data, i_endian);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_EQUAL(l_data,772);
    CU_ASSERT_EQUAL(p_packet->ui_all_len,20);
    CU_ASSERT_EQUAL(p_packet->ui_remaining,9);
    CU_ASSERT_EQUAL(p_packet->pos_index,1);
    CU_ASSERT_EQUAL(p_packet->ui_skip_len,0);

    //确认当前解析节点
    p_node_tmp = p_packet->p_node_index;
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,1);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00",10) == 0);
    }

    //确认第一个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x00\x00\x00\x40\xE2\xC1\xCD\x06\x05\x04",10) == 0);
    }
    //确认第二个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 1);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,1);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_TRUE(memcmp(p_bytebuffer_tmp->p_buf,"\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00",10) == 0);
    }

    bytebuff_free(&p_packet);
    return;
}

int add_suite_bytebuff_get_long(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frcomm bytebuff_get_long()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);
    CU_add_test(pSuite, "case5", case5);
    CU_add_test(pSuite, "case6", case6);

    return FR_SUCC;
}
